﻿using System;
using System.Web;
using System.Data;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using WinnerCMS.Common;
using WinnerCMS.Model;
namespace WinnerCMS.Data
{
    public class BusinessDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(Business model)
        {
            using (Entities db = new Entities())
            {
                db.Business.Add(model);
                db.SaveChangesAsync().Wait();
            }

        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        public void Delete(int[] ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Business where ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.Business.Remove(x));
                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region 检查是否已经支持过
        /// <summary>
        /// 检查是否已经支持过
        /// </summary>
        /// <param name="id">内容Id</param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DateTime? CheckReaded(int id, int userId)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Business
                        where x.Indent == id && x.UserId == userId
                        select x.CreateTime;
                return q.FirstOrDefault();
            }
        }

        #endregion

        #region 绑定交易列表
        /// <summary>
        /// 绑定交易列表
        /// </summary>
        /// <param name="flag"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Business> BindList(byte flag, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                db.Configuration.LazyLoadingEnabled = false;
                var query = from x in db.Business.Include(x=>x.User)
                            where x.Flag == flag
                            select x;
                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }

        #endregion
    }
}